package com.mlnx.stream.listener.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.EcgInfo;
import com.mlnx.data.service.EcgDataService;
import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.device.pojo.dto.EcgInfoMsg;
import com.mlnx.device.service.DeviceStateService;
import com.mlnx.mptp.model.DeviceState;
import com.mlnx.mptp.model.ecg.ECGData;
import com.mlnx.mptp.model.ecg.ECGDeviceInfo;
import com.mlnx.stream.listener.EcgListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

import cn.hutool.core.date.DateUtil;

/**
 * Created by amanda.shan on 2019/10/27.
 */
@Component
public class EcgStoreListener implements EcgListener {

    private MyLog log = MyLog.getLog(getClass());

    @Autowired
    private EcgDataService ecgDataService;

    @Autowired
    private DeviceStateService deviceStateService;

    @Override
    public void receiveEcgInfo(EcgInfoMsg ecgInfoMsg) {
        if (ecgInfoMsg.getEcgData() != null) {

            EcgInfo ecg = new EcgInfo();

            ecg.setPatientId(ecgInfoMsg.getPatientId());
            ecg.setDeviceId(ecgInfoMsg.getDeviceId());
            ecg.setDeviceType(ecgInfoMsg.getDeviceType() + "");
            ecg.setStartTime(ecgInfoMsg.getPacketTime());

            ECGDeviceInfo info = ecgInfoMsg.getEcgDeviceInfo();
            if (info != null) {
                ecg.setEcgChannelType(info.getEcgChannelType());
                ecg.setSamplingRate(info.getSampling());
                ecg.setAmplification(info.getAmplification());
                ecg.setBatteryLevel(info.getBatteryLevel());
                ecg.setPei(info.getPei());
            }

            ECGData ecgData = ecgInfoMsg.getEcgData();
            if (ecgData != null) {
                ecg.setHeartRate(ecgData.getEcgHeart());
                ecg.setData(ecgData.getSuccessionData());
                ecg.setEncryData(ecgData.getEncrySuccessionData());
                ecg.setFilterData(ecgData.getFilterData());
            }

            log.debug("保存心电 病人ID：{} {}", ecg.getPatientId(),
                    DateUtil.formatDateTime(new Date(ecg.getStartTime())));
            ecgDataService.save(ecg);
        }
    }

    @Override
    public void startEcgPacket(Integer patientId, String deviceId) {

        DeviceStateRecord deviceState = new DeviceStateRecord();
        deviceState.setDate(new Date());
        deviceState.setDeviceId(deviceId);
        deviceState.setPatientId(patientId);
        deviceState.setDeviceState(DeviceState.DEVICE_ONLINE);
        deviceStateService.saveDeviceState(deviceState);
    }

    @Override
    public void stopEcgPacket(Integer patientId, String deviceId) {
        DeviceStateRecord deviceState = new DeviceStateRecord();
        deviceState.setDate(new Date());
        deviceState.setDeviceId(deviceId);
        deviceState.setPatientId(patientId);
        deviceState.setDeviceState(DeviceState.DEVICE_OFFLINE);
        deviceStateService.saveDeviceState(deviceState);
    }
}
